/**
 * 用户服务
 *
 * @file   UserService.ts
 * @author xiaojian
 * @date   2018-04-22
 */
import { Injectable } from '@angular/core';
import { RequestService } from '../../../cores/services/request.service';
import { ApiData } from '../../../cores/classes/api-data.class';
import { SearchParams } from '../../../cores/classes/search-params';
import { Goods, Warehous, GoodsSpecification, GoodsSpecificationDetail } from '../interfaces/warehous.interface';
import { Observable } from 'rxjs';
import { Pagination } from 'ng-tui';
import { map } from 'rxjs/operators';

@Injectable()
export class WarehousService {

    constructor(private request: RequestService) { }

    /**
     *仓库列表
     */
    searchGoods(pagination: Pagination, search: SearchParams): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseList/search', pagination.getpageDataWith(search.values));
    }
    /**
        * 获取部门列表
        */
    warehousedepartment(): Observable<ApiData> {
        return this.request.url('/managerapi/department/get');
    }
    /**
     * 添加仓库
     */
    insertwarehouse(warehous: Warehous): Observable<ApiData> {
        return this.request.post('/managerapi/warehouse/add', warehous);
    }
    /**
     * 修改仓库
     */
    updateinsertware(warehous: Warehous) {
        return this.request.put('/managerapi/warehouse/update', warehous);
    }
    /**
    * 仓库详情
    */
    getwarehouse(id: number): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseDetail/search', { id });
    }

    /**
     * 删除仓库
     */
    deletewarehouse(id: number): Observable<ApiData> {
        return this.request.get('/managerapi/warehouse/delete', { id });
    }



    /**
  *库区列表
  */
    searchRegion(pagination: Pagination, search: SearchParams): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseRegionList/search', pagination.getpageDataWith(search.values));
    }
    /**
         * 仓库下拉列表
         */
    addhouse(): Observable<ApiData> {
        return this.request.url('/managerapi/warehouse/get');
    }
       /**
     * 通过仓库获库区
     */
    getPerson(warehouseId: number): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseRegion/get', { warehouseId });
    }
     /**
     * 通过库区从获通道
     */
    warehouseChanne(warehouseRegionId: number): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseChannel/get', { warehouseRegionId });
    }
    /**
    * 添加库区
   */
    insertRegion(warehous: Warehous): Observable<ApiData> {
        return this.request.post('/managerapi/warehouseRegion/add', warehous);
    }
    /**
     * 修改库区
     */
    updateRegion(warehous: Warehous) {
        return this.request.put('/managerapi/warehouseRegion/update', warehous);
    }
    /**
     * 删除库区
     */
    deleteRegion(id: number): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseRegion/delete', { id });
    }
    /**
    * 库区详情
    */
    getRegion(id: number): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseRegionDetail/search', { id });
    }




    /**
  *库位列表
  */
    searchLocationList(pagination: Pagination, search: SearchParams): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseLocationList/search', pagination.getpageDataWith(search.values));
    }
    /**
       * 添加库位
      */
    insertLocation(warehous: Warehous): Observable<ApiData> {
        return this.request.post('/managerapi/warehouseLocation/add', warehous);
    }
    /**
     * 修改库位
     */
    updateLocation(warehous: Warehous) {
        return this.request.put('/managerapi/warehouseLocation/update', warehous);
    }

/**
     * 删除库位
     */
    deleteLocation(id: number): Observable<ApiData> {
        return this.request.get('/managerapi/warehouseLocation/delete', { id });
    }
    /**
    * 库位详情
    */
   deldetlist(id: number): Observable<ApiData> {
    return this.request.get('/managerapi/warehouseLocationDetail/search', { id });
}


    /**
     * 删除商品
     */
    deleteGoods(goodsId: number): Observable<ApiData> {
        return this.request.delete('/store/goods/delete', { goodsId });
    }

    /**
     * 获取指定商品
     */
    getGoods(goodsId: number): Observable<ApiData> {
        return this.request.get('/store/goods/get', { goodsId });
    }

    /**
     * 添加商品
     */
    insertGoods(goods: Goods): Observable<ApiData> {
        return this.request.post('/store/goods/insert', goods);
    }

    /**
     * 更新商品
     */
    updateGoods(goods: Goods) {
        return this.request.put('/store/goods/update', goods);
    }

    /**
     * 更新商品规格
     */
    updateGoodsSpecification(
        goodsId: number,
        goodsSpecifications: GoodsSpecification[],
        goodsSpecificationDetails: GoodsSpecificationDetail[]
    ): Observable<ApiData> {
        return this.request.put('/store/goods/specification/update', { goodsId, goodsSpecifications, goodsSpecificationDetails });
    }

    /**
     * 获取商品下拉选项
     */
    getGoodsTypeOptions(): Observable<ApiData> {
        return this.request.url('/store/goods/type/options');
    }

    /**
     * 上传图片
     */
    // uploadGoodsImage(file: File): Observable<string> {
    //     return this.request.ossUpload('/managerapi/upload/image', file);
    // }
    uploadGoodsImage(file: File): Observable<string> {
        return this.request.files('/managerapi/upload/image', {}, [{ name: 'image', files: [file] }])
            .pipe(map<ApiData, string>(res => res.datas));
    }
}
